package com.itheima.health.controller.admin;

import com.itheima.health.common.MessageConst;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.result.Result;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.util.POIUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/ordersetting")
@Api(tags = "预约相关接口")
public class OrderSettingController {

    @Autowired
    private OrderSettingService orderSettingService;

    /**
     * Excel文件上传，并解析文件内容保存到数据库
     * @param excelFile
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("Excel文件上传，并解析文件内容保存到数据库")
    public Result upload(@RequestParam(value = "excelFile") MultipartFile excelFile){
        log.info("Excel文件上传，并解析文件内容保存到数据库: {}", excelFile);
        try {
            //读取Excel文件数据
            List<String[]> list = POIUtil.readExcel(excelFile);
            if(!CollectionUtils.isEmpty(list)) {
                List<OrderSetting> orderSettingList = new ArrayList<>();
                for (String[] strings : list) {
                    OrderSetting orderSetting = new OrderSetting(new Date(strings[0]), Integer.parseInt(strings[1])/*, Integer.parseInt(strings[2])*/);
                    orderSettingList.add(orderSetting);
                }
                orderSettingService.add(orderSettingList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.IMPORT_ORDERSETTING_FAIL);
        }
        return new Result(true,MessageConst.IMPORT_ORDERSETTING_SUCCESS);
    }

    /**
     * 根据日期查询预约设置数据
     * @param year
     * @param month
     * @return
     */
    @GetMapping("/getOrderSettingByMonth")
    @ApiOperation("根据日期查询预约设置数据")
    public Result getOrderSettingByMonth(Integer year, Integer month){//参数格式为：2023-05
        log.info("根据日期查询预约设置数据: {} {}", year, month);
        List<Map<Object,Object>> list = null;
        try{
            list = orderSettingService.getOrderSettingByMonth(year, month);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConst.GET_ORDERSETTING_FAIL);
        }
        return new Result(true,MessageConst.GET_ORDERSETTING_SUCCESS,list);
    }

    /**
     * 根据指定日期修改可预约人数
     * @param orderSetting
     * @return
     */
    @PostMapping("/editNumberByDate")
    @ApiOperation("根据指定日期修改可预约人数")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting) {
        log.info("根据指定日期修改可预约人数:{}", orderSetting);
        try {
            orderSettingService.editNumberByDate(orderSetting);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.ORDERSETTING_FAIL);
        }
        return new Result(true,MessageConst.ORDERSETTING_SUCCESS);
    }
}
